package com.example.onlinemarket.config;

import com.example.onlinemarket.interceptor.LoginInterceptor;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.module.SimpleModule;
import com.fasterxml.jackson.databind.ser.std.ToStringSerializer;
import org.springframework.boot.web.servlet.MultipartConfigFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.MediaType;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.util.unit.DataSize;
import org.springframework.web.servlet.config.annotation.ContentNegotiationConfigurer;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import javax.servlet.MultipartConfigElement;
import java.util.Arrays;
import java.util.List;

@Configuration
public class WebConfigurer implements WebMvcConfigurer {

    @Override//关闭后缀
    public void configureContentNegotiation(ContentNegotiationConfigurer config) {
        config.favorPathExtension(false);
    }

    @Override
    //配置登录拦截器
    public void addInterceptors(InterceptorRegistry registry) {
//        //拦截所有请求，通过访问的网址决定是否需要登录
//        registry.addInterceptor(LoginInterceptor())
//                .addPathPatterns("/**")
//                .excludePathPatterns("/","/login","/static/**","/css/*","/js/*","/mobile/**", "/good/**","/cache/**",  "/images/**");
    }

    @Bean
    //配置上传文件大小
    public MultipartConfigElement multipartConfigElement() {
        MultipartConfigFactory factory = new MultipartConfigFactory();
        factory.setMaxFileSize(DataSize.ofBytes(1024 * 1024 * 10)); // 限制上传文件大小
        return factory.createMultipartConfig();
    }

    @Override
    public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
        MappingJackson2HttpMessageConverter converter = new MappingJackson2HttpMessageConverter();
        ObjectMapper mapper = new ObjectMapper();
        //去除序列化中null和空字符串
        mapper.setSerializationInclusion(JsonInclude.Include.NON_ABSENT);
        //全局忽略实体类中不存在的字段
        mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
        //序列化时将数字转为String
        SimpleModule module = new SimpleModule();
        module.addSerializer(Integer.class, ToStringSerializer.instance);
        module.addSerializer(Integer.TYPE, ToStringSerializer.instance);

        mapper.registerModule(module);
        converter.setObjectMapper(mapper);
        List<MediaType> mediaTypes = Arrays.asList(
                MediaType.APPLICATION_JSON,
                MediaType.TEXT_PLAIN,
                MediaType.TEXT_HTML,
                MediaType.TEXT_XML,
                MediaType.APPLICATION_OCTET_STREAM);
        converter.setSupportedMediaTypes(mediaTypes);
        converters.add(converter);
    }

    @Bean
    public LoginInterceptor LoginInterceptor() {
        return new LoginInterceptor();
    }

}
